package com.hc.baselibray.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.example.baselibrary.R;

public class AlertDialog extends Dialog {
    private final AlterController mAltert;
    public AlertDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mAltert=new AlterController(this,getWindow());
    }
    public  static  class Builder{
        private final AlterController.AlertParams P;
        public Builder(Context context) {
            this(context, R.style.dialog);
        }

        public Builder(Context context,int themeResId) {
            P=new AlterController.AlertParams(context,themeResId);
        }
        //设置布局
        public  Builder setContentView(View view){
            P.mView=view;
            P.mViewLayoutResId=0;
            return this;
        }
        //设置布局layout id
        public  Builder setContentView(int layoutId){
            P.mView=null;
            P.mViewLayoutResId=layoutId;
            return this;
        }
        //设置文本
        public  Builder setText(int viewId,CharSequence text){
            P.mTextMap.put(viewId,text);
            return this;
        }
        //设置点击事件
        //setOnClickListener返回的是builder，不是dialog
        public Builder setOnClickListener(int view,View.OnClickListener listener){
            P.mClickArray.put(view,listener);
            return this;
        }
        public  Builder fullWidth(){
            P.mWidth= ViewGroup.LayoutParams.MATCH_PARENT;
            return this;
        }
        public  Builder formBottom(boolean isAnimation){
             if(isAnimation){
                 P.mAnimations=R.style.dialog_from_bottom_anim;
             }
             P.mGravity= Gravity.BOTTOM;
            return this;
        }
        public  Builder setWidthAndHeight(int width,int height){
            P.mWidth=width;
            P.mHeight=height;
            return this;
        }
        //设置默认动画
        public  Builder setDefaultAnnimation(){
            P.mAnimations=R.style.dialog_scle_anin ;
            return this;
        }
        //自定义动画
        public  Builder setAnimations(int styleAnimation){
            P.mAnimations=styleAnimation ;
            return this;
        }
        //create返回的是dialog，不是builder
        public  AlertDialog create() throws IllegalAccessException {
            final AlertDialog dialog=new AlertDialog(P.mContext,P.mThemeResId);
            P.apply(dialog.mAltert);
            dialog.setCancelable(P.mCancelable);
            if(P.mCancelable){
                dialog.setCancelable(true);
            }
            dialog.setOnCancelListener(P.mOnCancerListener);
            dialog.setOnDismissListener(P.mOnDismissListener);
//            if(P.mOnKeyListener!=null){
//
//            }
            return dialog;
        }
        //show返回的是dialog，不是builder
        public  AlertDialog show() throws IllegalAccessException {
            final  AlertDialog dialog=create();
            dialog.show();
            return dialog;
        }

    }

}
